package com.yimils.core.sys.controller;


import com.yimils.core.common.dto.RspBase;
import com.yimils.core.common.dto.SysCode;
import com.yimils.core.sys.entity.RoleEntity;
import com.yimils.core.sys.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class RoleController {
    @Resource(name = "roleService")
    private RoleService roleService;

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"","roles":[{"id":3,"name":"seo","title":"推广"},{"id":4,"name":"leader","title":"招商经理"},{"id":5,"name":"attach","title":"招商专员"}]}
     * @catalog 系统管理/角色管理
     * @title 获取角色表
     * @description 获取系统中所有角色
     * @method get
     * @url /roles
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @GetMapping("/roles")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase getRoleList(@RequestParam(required = false) Integer type) {
        RspBase rsp = RspBase.OK();
        rsp.put("roles", roleService.getRoleList(type));
        return rsp;
    }

    /**
     * showdoc
     *
     * @return {"code":0,"msg":"创建成功"}
     * @catalog 系统管理/角色管理
     * @title 添加角色
     * @description 添加系统中的角色
     * @method post
     * @url /role
     * @json_param {"name":"zsm","title":"测试招商经理"}
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @PostMapping("/role")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase createRole(@RequestBody RoleEntity reqParams) {
        if (roleService.createRole(reqParams)) {
            return RspBase.OK("创建成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "创建失败");
    }

    /**
     * showdoc
     * @param id 必选 PathVariable 角色id
     * @return {"code":0,"msg":"删除成功"}
     * @catalog 系统管理/角色管理
     * @title 删除角色
     * @description 删除系统中的角色
     * @method delete
     * @url /role/{id}
     * @return_param code   Integer     状态码
     * @return_param msg   String    消息
     * @remark 只有管理员有操作权限
     * @number 3
     */
    @DeleteMapping("/role/{id}")
    @RequiresRoles(value = {"admin", "ceo"}, logical = Logical.OR)
    public RspBase deleteRole(@PathVariable Long id) {
        if (roleService.deleteRole(id)) {
            return RspBase.OK("删除成功");
        }
        return RspBase.ERROR(SysCode.OPT_FAILURE, "删除失败");
    }
}
